package com.woniuxy.mesboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.mesboot.dto.OperInspDTO;
import com.woniuxy.mesboot.entity.MesOperationInspection;
import com.woniuxy.mesboot.service.OperationInspectionService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.OperInspVO;
import com.woniuxy.mesboot.vo.ProDetailsVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("operationInspection")
public class OperationInspectionController {
    @Resource
    private OperationInspectionService operationInspectionService;

    @PostMapping("queryAll")
    public Result queryAll(@RequestBody OperInspDTO inspDTO) {
        PageHelper.startPage(inspDTO.getCurrentPage(),inspDTO.getPageSize(),true);
        List<OperInspVO> i = operationInspectionService.selectAll(inspDTO);
        PageInfo<OperInspVO> pageInfo = new PageInfo<>(i);
        return Result.ok().data("list", i).data("total",pageInfo.getTotal());

    }
    @PostMapping("{operationProcedureId}")
    public Result queryDetails(@PathVariable("operationProcedureId") Integer operationProcedureId) {
        List<ProDetailsVO> detailsVOS = operationInspectionService.selectDetail(operationProcedureId);
        return Result.ok().data("detailsVOS", detailsVOS);

    }

    @PostMapping
    public Result insert(@RequestBody MesOperationInspection record) {
        int insert = operationInspectionService.insertSelective(record);
        if (insert > 0) {
            return Result.ok("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @DeleteMapping("{recordId}")
    public Result delete(@PathVariable("recordId") Long id) {
        int i = operationInspectionService.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("{recordId}")
    public Result queryById(@PathVariable("recordId") Long id) {
        MesOperationInspection inspection = operationInspectionService.selectByPrimaryKey(id);
        return Result.ok().data("item", inspection);
    }

    @PutMapping
    public Result update(@RequestBody MesOperationInspection record) {
        int i = operationInspectionService.updateByPrimaryKey(record);
        if (i > 0) {
            return Result.ok("修改成功");

        } else {
            return Result.error("修改失败");
        }
    }
}
